/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package datastorage;

import datamodel.Order;
import datamodel.Order2;
import datamodel.Order3;
import datamodel.Order4;
import datamodel.StorageInformer;
import java.util.Vector;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

/**
 *
 * @author Kvest
 */
public class OrderManagerStorage implements StorageInformer 
{
    private static String RMS_NAME = "ordersDB";
    
    public OrderManagerStorage()
    {
    }
    
    public void load(Vector orders)
    {
        try
        {
            RecordStore recordStore = RecordStore.openRecordStore(RMS_NAME, true);
            RecordEnumeration re = recordStore.enumerateRecords(null, null, false);
            try
            {
                if (re.numRecords() != 0)
                {
                    re.rebuild();
                    byte[] data;
                    while (re.hasNextElement())
                    {
                        int id = re.nextRecordId();
                        
                        try
                        {
                            data = recordStore.getRecord(id);
                            
                            if (data[data.length - 1] == Order4.ORDER_4)
                            {
                                orders.addElement(new Order4(data));
                            }
                            else if (data[data.length - 1] == Order3.ORDER_3)
                            {
                                orders.addElement(new Order3(data));
                            }
                            else if (data[data.length - 1] == Order2.ORDER_2)
                            {
                                orders.addElement(new Order2(data));
                            }
                            else if (data[data.length - 1] == Order.ORDER_1)
                            {
                                orders.addElement(new Order(data));
                            }
                        }catch(Exception e){}
                    }
                    data = null;
                }
            }
            finally
            {
                re.destroy();
                recordStore.closeRecordStore();
            }
        }catch(RecordStoreException rse){}
    }
    
    public void save(Vector orders)
    {
        //Удаляем хранилище
        try
        {
            RecordStore.deleteRecordStore(RMS_NAME);
        }catch(Exception e){}
        
        //Сохраняем заказы
        byte[] res;
        try
        {
            RecordStore recordStore = RecordStore.openRecordStore(RMS_NAME, true);
            try
            {
                int state;
                for(int i = orders.size() - 1; i >= 0; --i)
                {
                    state = ((Order)orders.elementAt(i)).getStatus();
                    if (state == Order.STATE_PERFORMED || state == Order.STATE_MISSED || state == Order.STATE_CANCELLED)
                    {
                        res = ((Order)orders.elementAt(i)).toByte();
                        recordStore.addRecord(res,0,res.length);
                    }
                }
            }
            finally
            {
                recordStore.closeRecordStore();
            }
        }catch(RecordStoreException rse){}
    }
    
    public void clear()
    {
        //Удаляем хранилище
        try
        {
            RecordStore.deleteRecordStore(RMS_NAME);
        }catch(Exception e){}
    }
    
    public int getUsedMemorySize()
    {
        int result = 0;
        try
        {
            RecordStore recordStore = RecordStore.openRecordStore(RMS_NAME, true);
            try
            {
                result = recordStore.getSize();
            }
            finally
            {
                recordStore.closeRecordStore();
            }
        }catch(RecordStoreException rse){}
        
        return result;
    }
    
    
    public int getFreeMemorySize()
    {
        int result = 0;
        try
        {
            RecordStore recordStore = RecordStore.openRecordStore(RMS_NAME, true);
            try
            {
                result = recordStore.getSizeAvailable();
            }
            finally
            {
                recordStore.closeRecordStore();
            }
        }catch(RecordStoreException rse){}
        
        return result;
    }
}
